package com.tablefood.dao.Impl;

import java.lang.reflect.ParameterizedType;
import java.util.List;
 






import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
 


import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tablefood.dao.UserDao;
import com.tablefood.domain.ShopUser;
import com.tablefood.utils.PageUserBean;
 

public class UserDaoImpl implements UserDao {
	
	@Resource
	private SessionFactory sessionFactory;   
   
	//添加一个新用户
	@Override 
	public boolean addUser(ShopUser user) { 
		System.out.println("添加/插入部分"); 
		
		if(checkExists(user))
			return false;
		
		try {
			
			sessionFactory.getCurrentSession().save(user);  
		
		}catch(Exception e) { 
			
			e.printStackTrace(); 
			System.out.println("添加失败");
			return false;
		}
		
		System.out.println("添加/插入结束-添加成功");
		return true;
	}
	
	//删除,只是更改用户登录权限
	@Override
	public boolean delUser(Long userid) {
		
		ShopUser user = (ShopUser) sessionFactory.getCurrentSession().load(ShopUser.class, userid);
		user.setStatus("0");
		sessionFactory.getCurrentSession().saveOrUpdate(user);
		
		return true;
	}
	
	//更新用户资料
	@Override
	public boolean updateUser(ShopUser user) {
		try{
			sessionFactory.getCurrentSession().saveOrUpdate(user);;  
		}catch(Exception e) { 
			System.out.println("更新失败");
			return false ;
		}
		System.out.println("---更新成功---");
		return true;
	}
	 
	
	//通过用户名查找用户
	@Override
	public List searchUser(String username) {
		
		String sql = "from ShopUser as user where user.username=?";
		System.out.println("查找用户基本语句："+sql);
		
		Query query = sessionFactory.getCurrentSession().createQuery(sql); 
		query.setString(0, username);
		
		System.out.println("---查询结果----");
		System.out.println("是否有结果query.list().size()："+query.list().size()); 
		System.out.println("返回result");
		List result = query.list(); 
		return result;
	}
	
	// 通过用户id查找用户
	@Override
	public ShopUser getUserByUserId(Long userid) {
		
		String hql = "FROM ShopUser AS u WHERE u.userid = ?"; 
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setLong(0, userid);  
		ShopUser user = (ShopUser) query.uniqueResult(); 
		
		if (user != null) {
			return user;
		}else{
			return null;
		}
	}
	
	
	
	//分页查找
	@Override
	public List<ShopUser> queryForPage(String hql, int offset, int length) {
		
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setFirstResult(offset);
		query.setMaxResults(length);
		List<ShopUser> list = query.list();
		
		return list;
	}
	
	//统计有多少
	@Override
	public int getAllRowCount(String hql) {
		
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		
		return query.list().size();
	}
	
	//检索重复用户名
	@Override
	public boolean checkUserName(String name) {
		
		String hql = "FROM ShopUser AS u WHERE u.username = ?"; 
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, name);  
		List<ShopUser> user = query.list();
		if (user.size() > 0) {
			return true;
		}else{
			return false;
		}
	}
	
	//检索重复邮箱
	public boolean checkEmail(String email){
		String hql = "FROM ShopUser AS u WHERE u.useremail = ?"; 
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, email);
		List<ShopUser> list = query.list();
		if (list.size() > 0) {
			return true;
		}else{
			return false;
		}
	}
	
	//检测是否重复
	public boolean checkExists(ShopUser user){
		
		boolean name = checkUserName(user.getUsername());
		boolean email = checkEmail(user.getUseremail());
		
		if(name||email)
			return true;
	 
		return false;
		 
		
	}
	
}
